package com.cf.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cf.domain.ResponseResult;
import com.cf.domain.pojo.Cooper;
import com.cf.exception.MyException;
import com.cf.service.ICooperService;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @Author lzqinga@126.com
 * @Date 2024/9/26  19:53
 */
@RestController
@RequestMapping("/cooper")
@AllArgsConstructor
public class CooperController {

    private final ICooperService cooperService;

    @GetMapping("page")
    public ResponseResult page(){
        Page<Cooper> data = cooperService.page(new Page<Cooper>());
        return ResponseResult.sucess(data);
    }

    @PostMapping
    public ResponseResult save(@RequestParam(value = "name", required = false)  String name,
                               @RequestParam("cooperType") Integer cooperType,
                               @RequestParam("personName") String personName,
                               @RequestParam(value = "position", required = false) String position,
                               @RequestParam("mobile") String mobile,
                               @RequestParam("type") Integer type,
                               @RequestParam("detail") String detail,
                               @RequestParam(value = "projectUrl", required = false) String projectUrl,
                               @RequestParam(value = "implementUrl", required = false) String implementUrl){
        if(type == 4){
            if(StringUtils.isBlank(projectUrl)){
                throw new MyException("请上传合作方案!");
            }
            if(StringUtils.isBlank(implementUrl)){
                throw new MyException("请上传项执行方案!");
            }
        }
        Cooper cooper = new Cooper();
        cooper.setName(name);
        cooper.setCooperType(cooperType);
        cooper.setPersonName(personName);
        cooper.setPosition(position);
        cooper.setMobile(mobile);
        cooper.setType(type);
        cooper.setDetail(detail);
        cooper.setProjectUrl(projectUrl);
        cooper.setImplementUrl(implementUrl);
        cooperService.save(cooper);
        return ResponseResult.sucess(null);
    }
}
